#!/bin/tcsh
# Every n seconds, check for new TREX logs and transfer them via DTN.
# 

# Set bundle expiration time
set EXPSEC=86400

if ($#argv != 3) then
  echo "usage: $0:t logDir filename-pattern dtn-dest"
  echo "(enclose filename-pattern in single-quotes)"
  exit
endif

set logDir = $argv[1]
set pattern = $argv[2]:q
set dtnDest = $argv[3]

set sleepSec = 10

set timefile = /tmp/timefile.$$
touch $timefile

while (1) 

  echo "check for new files in $logDir matching " $pattern:q
  echo "more recent than this file:"
  ls -l $timefile
  set newfiles = `find $logDir -name $pattern:q -newer $timefile`
  touch $timefile
  echo "$#newfiles new files: " $newfiles
  # Wait a bit for files to be completed
  sleep 10
  foreach file ($newfiles)
    echo process $file
    # Send file via DTN
    dtncp --expiration $EXPSEC $file $dtnDest
  end
  sleep $sleepSec
end

